package com.quwan.manager.controller;

import com.quwan.app.service.OrderService;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.manager.controller.dto.DispatchApplyDto;
import com.quwan.manager.controller.dto.DispatchDto;
import com.quwan.manager.controller.dto.OrderDTO;
import com.quwan.manager.controller.dto.PublishOrderListDTO;
import com.quwan.manager.service.ManagerOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author quan
 * @date 2021-07-28 18:14
 */
@Api(tags = "后台订单接口")
@RestController("managerOrder")
@RequiredArgsConstructor
@RequestMapping("mgr")
public class OrderController {
    private final OrderService orderService;
    private final ManagerOrderService managerOrderService;

    @ApiOperation("订单列表")
    @PostMapping("/orderList")
    public ApiResult<PageResult<OrderDTO>> orderList(@RequestParam(defaultValue = "1") int page,
                                                     @RequestParam(defaultValue = "10") int size,
                                                     @ApiParam(value = "发单人") @RequestParam(required = false) String p_member,
                                                     @ApiParam(value = "消费者") @RequestParam(required = false) String c_member,
                                                     @ApiParam(value = "发单人Id") @RequestParam(required = false) String publishMemberId,
                                                     @ApiParam(value = "消费者Id") @RequestParam(required = false) String consumerMemberId,
                                                     @ApiParam(value = "分类 id") @RequestParam(required = false) String category,
                                                     @ApiParam(value = "传数值 0未接单 1已接单 2进行中 3已完成 4订单取消 5中场取消") @RequestParam(required = false) String orderStatus,
                                                     @ApiParam(value = "示例：2022-22-22") @RequestParam(required = false) String beginTime,
                                                     @ApiParam(value = "2022-22-22") @RequestParam(required = false) String endTime,
                                                     @RequestParam(required = false) String address,
                                                     @RequestParam(required = false) String orderId,
                                                     @RequestParam(required = false) String publishBeginTime,
                                                     @RequestParam(required = false) String publishEndTime) {
        PageResult<OrderDTO> res =
                orderService
                        .getOrderList(page, size, orderId, p_member, c_member,
                                category, address, orderStatus, beginTime, endTime, publishBeginTime, publishEndTime,publishMemberId,consumerMemberId);
        return ApiResult.success(res);
    }

    @ApiOperation("发单列表")
    @PostMapping("/publish/order/list")
    public ApiResult<PageResult<PublishOrderListDTO>> publishOrderList(@RequestParam(defaultValue = "1") int pageNo,
                                                                       @RequestParam(defaultValue = "10") int pageSize,
                                                                       @ApiParam("发单id") @RequestParam(value = "id", required = false) String id,
                                                                       @ApiParam("发单人") @RequestParam(value = "name", required = false) String publishOrderMember,
                                                                       @ApiParam("发单人Id") @RequestParam(value = "mId", required = false) String mId,
                                                                       @ApiParam("分类id") @RequestParam(value = "cid", required = false) String categoryId,
                                                                       @ApiParam("状态 0关闭 1正常 2交易完成") @RequestParam(value = "status", required = false) String status,
                                                                       @ApiParam("排序") @RequestParam(value = "sort", required = false) String order,
                                                                       @ApiParam("0未审核 1已审核") @RequestParam(defaultValue = "0") String audit,
                                                                       @ApiParam("发单内容") @RequestParam(required = false) String content) {
        return ApiResult.success(managerOrderService.getPublishOrderList(pageNo, pageSize, publishOrderMember, categoryId, status, order, audit, mId, content,id));
    }

    // @ApiOperation("处理取消订单")
    //@PostMapping("/orderCancel")
    public ApiResult confirmOrderCancel(@ApiParam("消费订单id") @RequestParam String orderId,
                                        @ApiParam("同意 1  不同意0") @RequestParam String type) {
        boolean success = managerOrderService.cancelOrder(orderId, type);
        if (success)
            return ApiResult.success();
        return ApiResult.failed();
    }

    @ApiOperation("发单——关闭")
    @PostMapping("/orderPublishCancel")
    public ApiResult orderPublishCancel(@ApiParam("发单id") @RequestParam String orderId,
                                        @ApiParam("状态") @RequestParam Integer status) {
        boolean success = managerOrderService.orderManagerPublishCancel(orderId, status);
        if (success)
            return ApiResult.success();
        return ApiResult.failed();
    }

    @ApiOperation("发单——审核通过")
    @PostMapping("publish/order/pass")
    public ApiResult orderPass(@ApiParam("发单id") @RequestParam String id) {
        boolean success = managerOrderService.orderPass(id);
        if (success)
            return ApiResult.success();
        return ApiResult.failed();
    }

    @ApiOperation("发单——排序修改")
    @PostMapping("/orderPublishOrderNum")
    public ApiResult orderPublishChangeOrderNum(@ApiParam("发单id") @RequestParam String orderId,
                                                @ApiParam("排序") @RequestParam Integer orderNum) {
        boolean success = managerOrderService.orderPublishChangeOrderNum(orderId, orderNum);
        if (success)
            return ApiResult.success();
        return ApiResult.failed();
    }

    @ApiOperation("派单列表")
    @PostMapping("/dispatch/orderList")
    public ApiResult<PageResult<DispatchDto>> dispatchOrderList(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size,
                                                                @ApiParam("订单编号") @RequestParam(required = false) String orderId,
                                                                @ApiParam("派单人") @RequestParam(value = "dname", required = false) String dispatchMember,
                                                                @ApiParam("接单人") @RequestParam(value = "sname", required = false) String serviceMember,
                                                                @ApiParam("类型") @RequestParam(value = "cate", required = false) String categoryId,
                                                                @ApiParam("内容") @RequestParam(value = "content", required = false) String content,
                                                                @ApiParam("订单状态 -1未审核 1审核") @RequestParam(value = "status", required = false) String status,
                                                                @ApiParam("排序") @RequestParam(value = "sort", required = false) String sort,
                                                                @ApiParam("审核") @RequestParam(value = "audit", required = true) Integer audit
    ) {
        PageResult<DispatchDto> ret = managerOrderService.dispatchOrderList(page, size, orderId, dispatchMember, serviceMember, categoryId, content, status, sort,audit);
        return ApiResult.success(ret);

    }

    @ApiOperation("派单订单状态修改")
    @PostMapping("/dispatch/changeStatus")
    public ApiResult<String> dispatchChangeStatus(@RequestParam String id, @RequestParam Integer status) {
        return managerOrderService.dispatchChangeStatus(id, status) ? ApiResult.success() : ApiResult.error("修改失败");

    }

    @ApiOperation("派单——排序修改")
    @PostMapping("/dispatch/orderNum")
    public ApiResult dispatchOrderNum(@ApiParam("id") @RequestParam String id,
                                      @ApiParam("排序") @RequestParam Integer orderNum) {
        boolean success = managerOrderService.updDispatchOrderOrderNum(id, orderNum);
        if (success)
            return ApiResult.success();
        return ApiResult.failed();
    }

    @ApiOperation("派单——申请列表")
    @PostMapping("/dispatch/apply/list")
    public ApiResult<PageResult<DispatchApplyDto>> dispatchApplyList(@RequestParam(defaultValue = "1") int page,
                                                                     @RequestParam(defaultValue = "10") int size,
                                                                     @ApiParam("派单id") @RequestParam String id) {
        PageResult<DispatchApplyDto> ret = managerOrderService.dispatchApplyList(page,size,id);
        return ApiResult.success(ret);
    }
    @ApiOperation("派单取消")
    @PostMapping("/dispatch/cancel")
    public ApiResult cancelDispatchOrder(@ApiParam("派单id") @RequestParam String id,
                                         @ApiParam("0全额退款  1扣除费用") @RequestParam Integer type) {
         managerOrderService.cancelDispatchOrder(id,type);
        return ApiResult.success();
    }

    @ApiOperation("发单取消")
    @PostMapping("/publish/cancel")
    public ApiResult cancelPublishOrder(@ApiParam("发单id") @RequestParam String id,
                                         @ApiParam("0全额退款  1扣除费用") @RequestParam Integer type) {
        managerOrderService.cancelPublishOrder(id,type);
        return ApiResult.success();
    }

}
